<template>
  <div class="mod-shop-notice">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="type">
        <el-tag v-if="scope.row.type === 1" size="small" type="success">{{ $t('shop.platformNotice') }}</el-tag>
        <el-tag v-if="scope.row.type === 2  " size="small">{{ $t('shop.mallNotice') }}</el-tag>
      </template>

      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small" type="danger">{{$t('publics.cancel')}}</el-tag>
        <el-tag v-else size="small">{{$t('publics.publicar')}}</el-tag>
      </template>
      <template slot-scope="scope" slot="isTop">
        <el-tag v-if="scope.row.isTop === 0" size="small">{{$t('publics.no')}}</el-tag>
        <el-tag v-else size="small">{{$t('publics.yes')}}</el-tag>
      </template>
      <template slot="menuLeft">
        <el-button
          v-if="isAuth('platform:notice:save')"
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
        >{{$t('sysManagement.add')}}</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="isAuth('platform:notice:update')"
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row.id)"
        >{{$t('sysManagement.modify')}}</el-button>
        <el-button
          v-if="isAuth('platform:notice:delete')"
          type="danger"
          icon="el-icon-delete"
          size="small"
          @click.stop="deleteHandle(scope.row.id)"
        >{{$t('remindPop.delete')}}</el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/platform/notice'
import AddOrUpdate from './notice-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/notice/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page == null ? this.page.currentPage : page.currentPage,
          size: page == null ? this.page.pageSize : page.pageSize
        }, this.searchForm))
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('seckill.isDeleOper'), this.$i18n.t('remindPop.remind'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/platform/notice/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调用
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.getDataList(this.page)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
